﻿using System;
using System.Text;

namespace DataStructure
{
    public class Array
    {
        private int[] array;
        private int capacity = 10;
        private int size;

        public int Length
        {
            get { return size; }
        }

        public bool IsEmpty
        {
            get { return size == 0; }
        }

        public Array()
        {
            array = new int[10];
            size = 0;
        }

        public Array(int capacity)
        {
            this.capacity = capacity;
            array = new int[capacity];
            size = 0;
        }

        public int Get(int index)
        {
            if (index < 0 || index > size - 1)
            {
                throw new IndexOutOfRangeException("索引超出界限");
            }

            return array[index];
        }

        public void Update(int value, int index)
        {
            if (index < 0 || index > size - 1)
            {
                throw new IndexOutOfRangeException("索引超出界限");
            }

            array[index] = value;
        }

        public void Add(int value)
        {
            if (size == capacity)
            {
                AddByIndexOutOfRange(value);
            }
            else
            {
                array[size] = value;
            }

            size++;
        }

        public void Insert(int value, int index)
        {
            if (index < 0 || index > size - 1)
            {
                throw new IndexOutOfRangeException("索引超出界限");
            }

            if (size != capacity)
            {
                for (int i = size - 1; i >= size; i--)
                {
                    array[i + 1] = array[i];
                }
            }
            else
            {
                InsertByIndexOutOfRange(index);
            }

            array[index] = value;
            size++;
        }

        public void Delete(int index)
        {
            if (index < 0 || index > size - 1)
            {
                throw new IndexOutOfRangeException("索引超出界限");
            }

            for (; index < size - 1; index++)
            {
                array[index] = array[index + 1];
            }

            size--;
        }

        void AddByIndexOutOfRange(int value)
        {
            capacity += 1;
            int[] newArray = new int[capacity];
            array.CopyTo(newArray, 0);
            array = newArray;
            array[capacity - 1] = value;
        }

        void InsertByIndexOutOfRange(int index)
        {
            capacity += 1;
            int[] newArray = new int[capacity];
            System.Array.Copy(array, 0, newArray, 0, index);
            System.Array.Copy(array, index, newArray, index + 1, array.Length - index);
            array = newArray;
        }

        public override string ToString()
        {
            Console.Write("数组元素: ");
            StringBuilder sr = new StringBuilder();
            sr.Append("[");

            for (int i = 0; i < size; i++)
            {
                sr.Append(array[i]);
                if (i != size - 1)
                {
                    sr.Append(" ");
                }
            }

            sr.Append("]");
            return sr.ToString();
        }
    }

    class Progrm
    {
        static void Main1(string[] args)
        {
            Array array = new Array();

            for (int i = 0; i < 10; i++)
            {
                array.Add(i);
            }

            array.Insert(11111111, 3);
            Console.WriteLine(array.ToString());
            array.Delete(3);
            Console.WriteLine(array.ToString());
        }
    }
}